package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/right-triangles/description/
 * @create 2025/8/15 10:37
 * @since JDK17
 */

public class Solution15 {
    public long numberOfRightTriangles(int[][] grid) {
        long ans = 0;
        int n = grid.length, m = grid[0].length;
        int[] row = new int[n], col = new int[m];
        // 算出行
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                row[i]++;
            }
        }
        // 算出列
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                col[i]++;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] != 0) {
                    ans += row[i] * col[j];
                }
            }
        }
        return ans;
    }
}
